package com.manage.function.service.impl;

import java.util.List;

import com.manage.common.core.domain.entity.SysUser;
import com.manage.common.utils.SecurityUtils;
import com.manage.function.domain.FunctionAsset;
import com.manage.function.domain.vo.Constants;
import com.manage.function.mapper.FunctionAssetMapper;
import com.manage.system.domain.SysUserRole;
import com.manage.system.mapper.SysUserMapper;
import com.manage.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.manage.function.mapper.FunctionAssetApplicationMapper;
import com.manage.function.domain.FunctionAssetApplication;
import com.manage.function.service.IFunctionAssetApplicationService;

import javax.annotation.Resource;

/**
 * 资产申请Service业务层处理
 *
 * @author jinghj
 * @date 2025-03-09
 */
@Service
public class FunctionAssetApplicationServiceImpl implements IFunctionAssetApplicationService
{
    @Resource
    private FunctionAssetApplicationMapper functionAssetApplicationMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Resource
    private FunctionAssetMapper functionAssetMapper;

    /**
     * 查询资产申请
     *
     * @param id 资产申请主键
     * @return 资产申请
     */
    @Override
    public FunctionAssetApplication selectFunctionAssetApplicationById(Long id)
    {

        return functionAssetApplicationMapper.selectFunctionAssetApplicationById(id);
    }

    /**
     * 查询资产申请列表
     *
     * @param functionAssetApplication 资产申请
     * @return 资产申请
     */
    @Override
    public List<FunctionAssetApplication> selectFunctionAssetApplicationList(FunctionAssetApplication functionAssetApplication)
    {
        String username = SecurityUtils.getUsername();
        SysUser sysUser = sysUserMapper.selectUserByUserName(username);
        Long userId = sysUser.getUserId();
        SysUserRole sysUserRole = sysUserRoleMapper.selectSysUserRoleByUserId(userId);
        if (sysUserRole.getRoleId()!=1){
            functionAssetApplication.setUserName(username);
            functionAssetApplicationMapper.selectFunctionAssetApplicationList(functionAssetApplication);
        }
        return functionAssetApplicationMapper.selectFunctionAssetApplicationList(functionAssetApplication);
    }

    /**
     * 新增资产申请
     *
     * @param functionAssetApplication 资产申请
     * @return 结果
     */
    @Override
    public int insertFunctionAssetApplication(FunctionAssetApplication functionAssetApplication)
    {
        functionAssetApplication.setProgress(Constants.ADMIN_IN_PROGRESS);
        functionAssetApplication.setStatus(Constants.ADMIN_IN_PROGRESS_STATUS);
        return functionAssetApplicationMapper.insertFunctionAssetApplication(functionAssetApplication);
    }

    /**
     * 修改资产申请
     *
     * @param functionAssetApplication 资产申请
     * @return 结果
     */
    @Override
    public int updateFunctionAssetApplication(FunctionAssetApplication functionAssetApplication)
    {
        if (functionAssetApplication.getStatus().equals(Constants.ADMIN_PASS_STATUS)){
            functionAssetApplication.setProgress(Constants.ADMIN_PASS);
        }else if (functionAssetApplication.getStatus().equals(Constants.ADMIN_NOT_PASS_STATUS)){
            functionAssetApplication.setProgress(Constants.ADMIN_NOT_PASS);
            Long id = functionAssetApplication.getId();
            FunctionAssetApplication functionAssetApplication1 = functionAssetApplicationMapper.selectFunctionAssetApplicationById(id);
                Long assetId = functionAssetApplication1.getAssetId();
                FunctionAsset functionAsset = functionAssetMapper.selectFunctionAssetById(assetId);
                functionAsset.setQuantity(functionAsset.getQuantity()+functionAssetApplication1.getQuantity());
                functionAssetMapper.updateFunctionAsset(functionAsset);

        }
        return functionAssetApplicationMapper.updateFunctionAssetApplication(functionAssetApplication);
    }

    /**
     * 批量删除资产申请
     *
     * @param ids 需要删除的资产申请主键
     * @return 结果
     */
    @Override
    public int deleteFunctionAssetApplicationByIds(Long[] ids)
    {
        return functionAssetApplicationMapper.deleteFunctionAssetApplicationByIds(ids);
    }

    /**
     * 删除资产申请信息
     *
     * @param id 资产申请主键
     * @return 结果
     */
    @Override
    public int deleteFunctionAssetApplicationById(Long id)
    {
        return functionAssetApplicationMapper.deleteFunctionAssetApplicationById(id);
    }
}
